package ims.servlet;

import ims.beans.ProductType;
import ims.constants.IMSConstants;
import ims.db.delete.prodType.DeleteProdTypeDAO;
import ims.db.delete.prodType.DeleteProdTypeDbRequest;
import ims.db.delete.prodType.DeleteProdTypeDbResponse;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeleteProdTypeServlet
 */
@WebServlet("/DeleteProdTypeServlet")
public class DeleteProdTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteProdTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			String [] prod = request.getParameter("prodType").split(":");
	
			//Retrieve all product types
			DeleteProdTypeDbRequest dbRequest = new DeleteProdTypeDbRequest();
			ProductType prodType = new ProductType();
			prodType.setProdTypeId(prod[0]);
			prodType.setProdType(prod[1]);
			dbRequest.setProdType(prodType);
			DeleteProdTypeDAO db = new DeleteProdTypeDAO();
			DeleteProdTypeDbResponse dbResponse = (DeleteProdTypeDbResponse) db.execute(dbRequest);
			
			String nextLoc = "/IMS/prodType?"+IMSConstants.RESPONSE_HEADER+"="+dbResponse.getMsg();      
			response.sendRedirect(response.encodeRedirectURL(nextLoc));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
